package com.ggs.rg.ybjb.adminmodule.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ggs.rg.ybjb.adminmodule.service.TOrderService;
import com.ggs.rg.ybjb.adminmodule.service.UserService;
import com.ggs.rg.ybjb.adminmodule.util.UserUtil;
import com.ggs.rg.ybjb.pojomodule.pojo.TOrder;
import com.ggs.rg.ybjb.pojomodule.pojo.TUser;
import com.ggs.rg.ybjb.pojomodule.util.CommonUtil;
import com.ggs.rg.ybjb.pojomodule.util.StringTools;
import com.ggs.rg.ybjb.pojomodule.util.constants.ErrorEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:UserController
 * Package:com.ggs.rg.ybjb.adminmodule.controller
 * Description:
 *
 * @Date:2021/11/6 9:04
 * @Author:cxw
 */
@RestController
@RequestMapping("/userModule")
public class UserController
{

    @Autowired
    UserService userService;

    @Autowired
    TOrderService tOrderService;

    @Value("${server.port}")
    private String port;

    /**
     * 获取用户的基本信息,并分页
     * @param pagesNumber 页码,可以不传默认为1
     * @return
     */
    @GetMapping("/getUsers")
    public JSONObject getUsers(@RequestParam(value = "pagesNumber") Integer pagesNumber){
        //创建分页对象
        Page<TUser> page = new Page<>(pagesNumber,12);
        //创建条件构造器
        QueryWrapper<TUser> wrapper = new QueryWrapper<TUser>();
        //向条件构造器加入条件
        wrapper.select("phone,name,type,create_time,update_time").orderByDesc("update_time");
        //分页查询
        Page<TUser> result = userService.page(page, wrapper);
        //获取分页之后总页数
        long pagesAmount = result.getPages();
        List<TUser> userData = result.getRecords();
        JSONObject resultJson = new JSONObject();
        //调用用户工具类,返回一个去除空信息的用户信息list
        List<JSONObject> resultList = UserUtil.getResultList(userData);
        resultJson.put("pagesAmount",pagesAmount);
        resultJson.put("pagesNumber",pagesNumber);
        resultJson.put("allUserMessage",resultList);
        return CommonUtil.successJson(resultJson);
    }

    /**
     * 获取系统总用户数
     * @return
     */
    @GetMapping("/getUserCount")
    public JSONObject getUserCount(){
        return CommonUtil.successJson(userService.count());
    }

    /**
     * 根据手机号获取用户详细信息,包括用户订单信息,有多少订单给多少
     * @param phone 用户手机号
     * @return
     */
    @GetMapping("/getUserDeatilById")
    public JSONObject getUserDeatilById(@RequestParam(required = false) String phone){
//        if (StringTools.isNullOrEmpty(phone))
//            return CommonUtil.errorJson(ErrorEnum.E_90003);
        //获得用户信息
        TUser user = userService.getById(phone);
        if (StringTools.isNullOrEmpty(user))
            return CommonUtil.sqlErrorJson("查询不到指定用户");
        List<TOrder> list = tOrderService.selectTOrderByPhone(phone);
//        list.forEach(System.out::println);
        if (list.size() == 0)
            return CommonUtil.successJson(UserUtil.getResultList(user,"9002"));
        List<JSONObject> orderList = new ArrayList<>();
        for (TOrder order:list){
            if (order.getStatus() == 1){
                JSONObject jsonObject = tOrderService.selectTOrderDetailById(Long.valueOf(order.getOid()));
                orderList.add(jsonObject);
            }else {
                JSONObject jsonObject = tOrderService.selectUnpaidTOrderDetailById(Long.valueOf(order.getOid()));
                orderList.add(jsonObject);
            }
        }
        return CommonUtil.successJson(UserUtil.getResultList(user,orderList,port));
//        return null;
    }
}
